/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: PhpSysInfo - Host XML-Query
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


var PHPSysInfo = new Array();
var PHPSysInfoDOM;

var debugImage;

// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["php_host"] = new Object();
myPlugins["php_host"]["name"] = getLocalizedString2("php_host");
myPlugins["php_host"]["onlyonce"] = false;
myPlugins["php_host"]["newObject"] = function (NewSettings) { return new plugin_php_host_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_php_host_sensor(NewSettings) {
	// Required variables
	this.plugin = "php_host";
	this.settings = NewSettings;
	this.edit = plugin_php_host_edit;
	this.ReDraw = plugin_php_host_ReDraw;
	//this.Delete = plugin_php_host_Delete;
	this.Update = plugin_php_host_update;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.id) this.settings.id = PHPSysInfo.length;
	if(!this.settings.url) this.settings.url = "http://tools.bluepaw.de/widgets/phpsysinfo/index.xml?template=xml";
	if(!this.settings.distro) this.settings.distro = 0;
	if(!this.settings.distrologo) this.settings.distrologo = 0;
	if(!this.settings.updspeed) this.settings.updspeed = 300;


	//The Sonsore
	this.host = new class_TextLine(this.frame,0,"");
	this.host.size = mod_sysmonitor_size * 1.2;
	this.host.data.style.textAlign = "center";
	this.host.data.hOffset = 0;
	this.host.data.width = mod_sysmonitor_width;
	this.host.data.data = "..."; // Some loading text needed here
	this.host.label.color = this.settings.color;
	this.host.data.color = this.settings.color;
		var shad = new Shadow();
		shad.color = "#000000";
		shad.opacity = 255;
		shad.hOffset = 1;
		shad.vOffset = 1;
		this.host.data.shadow = shad;
	this.url = new URL();

	this.logo = new Image();


	this.logo = new Image();
	this.logo.hOffset = 0;
	this.logo.vOffset = 0;
	this.logo.height = this.host.data.vOffset + (this.host.data.size * 1/3);
	this.logo.width = this.logo.height;
	this.logo.visible = false;
	this.frame.appendChild(this.logo);



	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_php_host_update.call(obj); };
	if(main.visible == true) plugin_php_host_update.call(obj);
else print("Host not updated on creation!");


	// Calculating sensore height (required!)
	this.frame.height = this.host.data.vOffset + (this.host.data.size * 1/3);
	return true;

// --------------------------------------------------------------------------------
// Helper Functions


	function plugin_php_host_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("php_host_url");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("php_host_urldesc");
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.url;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("php_host_distro");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.distro;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("php_host_distrologo");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.distrologo;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(5,30,45,60,180,300,600,900);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;




		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			var updatenow = false;

			this.settings.color = Drive_Edit_DialogResults[0];
			this.host.label.color = this.settings.color;
			this.host.data.color = this.settings.color;

			if(this.settings.distro != Drive_Edit_DialogResults[2]) {
				this.settings.distro = Drive_Edit_DialogResults[2];
				if(this.settings.distro == 1)
					this.host.data.data = PHPSysInfo[this.settings.id]["Vitals"]["Hostname"] + " (" + PHPSysInfo[this.settings.id]["Vitals"]["Distro"] + ")";
				else
					this.host.data.data = PHPSysInfo[this.settings.id]["Vitals"]["Hostname"];
			}

			if(this.settings.distrologo != Drive_Edit_DialogResults[3]) {
				this.settings.distrologo = Drive_Edit_DialogResults[3];
				if(this.settings.distrologo==0) {
					this.logo.visible = false;
				} else {
					this.logo.visible = true;
					updatenow = true;
				}
			}

			if ( this.settings.updspeed != Drive_Edit_DialogResults[4] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[4];
				this.timer.interval = this.settings.updspeed;
				updatenow = true;
			}

			if(this.settings.url != Drive_Edit_DialogResults[1]) {
				delete PHPSysInfo[this.settings.id];
				this.settings.url = Drive_Edit_DialogResults[1];
				updatenow = true;
			}

			if(updatenow) plugin_php_host_update.call(obj);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}


	function plugin_php_host_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.host.size = mod_sysmonitor_size * 1.2;
		this.host.data.hOffset = 0;
		this.host.data.width = mod_sysmonitor_width;

		this.logo.height = this.host.data.vOffset + (this.host.data.size * 1/3);
		this.logo.width = this.logo.height;

		this.frame.height = this.host.data.vOffset + (this.host.data.size * 1/3);
		return true;
	}


	function plugin_php_host_update() {
if(mod_sysmonitor_debug>3) print("plugin_PHP_host: Update");
		this.url.location = this.settings.url;
		this.url.fetchAsync( function() { plugin_php_host_update_done.call(obj) } );
		return true;
	}


	function plugin_php_host_update_done() {
		if(mod_sysmonitor_debug>2) print("plugin_php_host_update_done: " + this.url.response);
		if(this.url.response==200) {
			//if(PHPSysInfo[this.settings.id]) delete PHPSysInfo[this.settings.id];
			if(!PHPSysInfo[this.settings.id]) PHPSysInfo[this.settings.id] = new Object();
			PHPSysInfoDOM = XMLDOM.parse(this.url.result);

			// --- Version + time ---
			if(!PHPSysInfo[this.settings.id]["Generation"]) PHPSysInfo[this.settings.id]["Generation"] = new Object();
			var base = PHPSysInfoDOM.evaluate( "phpsysinfo/Generation/@*" );
			for(var i = 0; i < base.length; i++) {
				PHPSysInfo[this.settings.id]["Generation"][base.item(i).name] = base.item(i).nodeValue;
			}
			delete base;
			// --- Version + time ---

			// --- Vitals ---
			if(!PHPSysInfo[this.settings.id]["Vitals"]) PHPSysInfo[this.settings.id]["Vitals"] = new Object();
			var vit = PHPSysInfoDOM.evaluate( "phpsysinfo/Vitals" );
			var child = vit.item(0).firstChild;
			while ( child != null ) {
				PHPSysInfo[this.settings.id]["Vitals"][child.tagName] = child.firstChild.data;
				child = child.nextSibling;
			}
			delete vit;
			// --- Vitals ---


			// --- Hardware CPU ---
			if(!PHPSysInfo[this.settings.id]["CPU"]) PHPSysInfo[this.settings.id]["CPU"] = new Object();
			var cpu = PHPSysInfoDOM.evaluate( "phpsysinfo/Hardware/CPU" );
			child = cpu.item(0).firstChild;
			while ( child != null ) {
				PHPSysInfo[this.settings.id]["CPU"][child.tagName] = child.firstChild.data;
				child = child.nextSibling;
			}
			delete cpu;
			// --- Hardware CPU ---


			// --- Memory ---
			if(!PHPSysInfo[this.settings.id]["Memory"]) PHPSysInfo[this.settings.id]["Memory"] = new Object();
			var mem = PHPSysInfoDOM.evaluate( "phpsysinfo/Memory" );
			child = mem.item(0).firstChild;
			while ( child != null ) {
				PHPSysInfo[this.settings.id]["Memory"][child.tagName] = child.firstChild.data;
				child = child.nextSibling;
			}
			delete mem;
			// --- Memory ---


			// --- SWAP ---
			if(!PHPSysInfo[this.settings.id]["SWAP"]) PHPSysInfo[this.settings.id]["SWAP"] = new Object();
			var swap = PHPSysInfoDOM.evaluate( "phpsysinfo/Swap" );
			child = swap.item(0).firstChild;
			while ( child != null ) {
				PHPSysInfo[this.settings.id]["SWAP"][child.tagName] = child.firstChild.data;
				child = child.nextSibling;
			}
			delete swap;
			// --- SWAP ---


			// --- FileSystem ---
			if(!PHPSysInfo[this.settings.id]["FileSystem"]) PHPSysInfo[this.settings.id]["FileSystem"] = new Array();
			var mount = PHPSysInfoDOM.evaluate( "phpsysinfo/FileSystem/Mount" );
			for(var i = 0; i < mount.length; i++) {
					if(!PHPSysInfo[this.settings.id]["FileSystem"][i]) PHPSysInfo[this.settings.id]["FileSystem"][i] = new Object();
					child = mount.item(i).firstChild;
					while ( child != null ) {
						if(child.tagName == "Device") {
							PHPSysInfo[this.settings.id]["FileSystem"][i][child.tagName] = child.firstChild.firstChild.data;
						} else {
							PHPSysInfo[this.settings.id]["FileSystem"][i][child.tagName] = child.firstChild.data;
						}
						child = child.nextSibling;
					}
			        }
			// --- FileSystem ---


			// --- NetDevice ---
			if(!PHPSysInfo[this.settings.id]["NetDevice"]) PHPSysInfo[this.settings.id]["NetDevice"] = new Array();
			var net = PHPSysInfoDOM.evaluate( "phpsysinfo/Network/NetDevice" );
			for(var i = 0; i < net.length; i++) {
					if(!PHPSysInfo[this.settings.id]["NetDevice"][i]) PHPSysInfo[this.settings.id]["NetDevice"][i] = new Object();
					child = net.item(i).firstChild;
					while ( child != null ) {
						PHPSysInfo[this.settings.id]["NetDevice"][i][child.tagName] = child.firstChild.data;
						child = child.nextSibling;
					}
			        }
			delete net;
			// --- NetDevice ---


			// --- SCSI ---
			if(!PHPSysInfo[this.settings.id]["SCSI"]) PHPSysInfo[this.settings.id]["SCSI"] = new Array();
			var scsi = PHPSysInfoDOM.evaluate( "phpsysinfo/Hardware/SCSI/Device" );
			for(var i = 0; i < scsi.length; i++) {
					if(!PHPSysInfo[this.settings.id]["SCSI"][i]) PHPSysInfo[this.settings.id]["SCSI"][i] = new Object();
					child = scsi.item(i).firstChild;
					while ( child != null ) {
						PHPSysInfo[this.settings.id]["SCSI"][i][child.tagName] = child.firstChild.data;
						child = child.nextSibling;
					}
			        }
			delete scsi;
			// --- SCSI ---


			delete child;
			//delete PHPSysInfoDOM;

			// Update self
			this.host.data.data = PHPSysInfo[this.settings.id]["Vitals"]["Hostname"];

			if(this.settings.distro == 1) {
				this.host.data.data += " (" + PHPSysInfo[this.settings.id]["Vitals"]["Distro"] + ")";
			}
			if(this.settings.distrologo == 1) {
				if( dirname(this.settings.url) + "images/" + PHPSysInfo[this.settings.id]["Vitals"]["Distroicon"] != this.logo.src) {
					this.logo.src = dirname(this.settings.url) + "images/" + PHPSysInfo[this.settings.id]["Vitals"]["Distroicon"];
					this.logo.visible = true;
				}
			}


			// Update all other
			for(var i = 0;i < mySensorCount;i++) {
				if( mySensors[myOrder[i]].plugin.substr(0,5) == "phpp_" ) {
					if( mySensors[myOrder[i]].settings.id == this.settings.id ) {
						mySensors[myOrder[i]].Update();
					}
				}
			}

		} else {
		}
	}

	function plugin_php_host_Delete() {
		this.timer.ticking = 0;
		delete PHPSysInfo[this.settings.id];
		return true;
	}

function dirname(path) {
    return path.match( /.*\// );
}
function basename(path) {
    return path.replace( /.*\//, "" );
}

}

// --------------------------------------------------------------------------------
// The END